package com.example.xinwenkehuduan;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.example.xinwenkehuduan.Adapter.MyFragmentAdapter;
import com.example.xinwenkehuduan.Fragment.SheziFragment;
import com.example.xinwenkehuduan.Fragment.WoFragment;
import com.example.xinwenkehuduan.Fragment.ZhuyeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener , ViewPager.OnPageChangeListener{

    private ViewPager viewPager;
    private RadioButton rbShouye;
    private RadioButton rbShezhi;
    private RadioButton rbWo;
    private RadioGroup rgMenu;
    private FragmentManager manager;
    private List<Fragment>fragmentList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    private void initView() {
        manager=getSupportFragmentManager();
        initFragmentList();
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        rbShouye = (RadioButton) findViewById(R.id.rb_shouye);
        rbShezhi = (RadioButton) findViewById(R.id.rb_shezhi);
        rbWo = (RadioButton) findViewById(R.id.rb_wo);
        rgMenu = (RadioGroup) findViewById(R.id.rg_menu);
        rgMenu.setOnCheckedChangeListener(this);
        viewPager.addOnPageChangeListener(this);
        MyFragmentAdapter adapter = new MyFragmentAdapter(manager,0,fragmentList);
        viewPager.setAdapter(adapter);
    }
    private void initFragmentList() {
        ZhuyeFragment zhuyeFragment = new ZhuyeFragment();
        SheziFragment sheziFragment = new SheziFragment();
        WoFragment woFragment = new WoFragment();
        fragmentList = new ArrayList<>();
        fragmentList.add(zhuyeFragment);
        fragmentList.add(sheziFragment);
        fragmentList.add(woFragment);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.rb_shouye:
                viewPager.setCurrentItem(0);
                break;
            case R.id.rb_shezhi:
                viewPager.setCurrentItem(1);
                break;
            case R.id.rb_wo:
                viewPager.setCurrentItem(2);
                break;
        }

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position){
            case 0:
                rbShouye.setChecked(true);
                break;
            case 1:
                rbShezhi.setChecked(true);
                break;
            case 2:
                rbWo.setChecked(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
